//
//  ArticleClassifyAddViewController.m
//  reSearchDemo
//
//  Created by badboy on 15/10/26.
//  Copyright © 2015年 Kiwaro. All rights reserved.
//

#import "ArticleClassifyAddViewController.h"
#import "TextInput.h"

@interface ArticleClassifyAddViewController () {
    NSInteger actionType;   // 0为添加，1为编辑
}

@property (nonatomic, retain) OnePlatformModel *model;
@property (nonatomic, retain) ArticleClassifyModel *acModel;
@property (weak, nonatomic) IBOutlet KTextField *classifyName;    //类别名称

@end

@implementation ArticleClassifyAddViewController

- (id)initWithPlatformModel:(OnePlatformModel *)model {
    self = [super initWithNibName:@"ArticleClassifyAddViewController" bundle:nil];
    if (self) {
        self.model = model;
        self.title = @"添加文章分类";
        actionType = 0;
    }
    return self;
}

- (id)initWithPlatformModel:(OnePlatformModel *)pmodel acModel:(ArticleClassifyModel *)acModel {
    self = [super initWithNibName:@"ArticleClassifyAddViewController" bundle:nil];
    if (self) {
        self.model = pmodel;
        self.acModel = acModel;
        self.title = @"编辑文章分类";
        actionType = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.classifyName.text = self.acModel ? self.acModel.aClassifyName : nil;
}

- (IBAction)sureBtnAction:(UIButton *)sender {
    NSString *name = self.classifyName.text;
    if (name.length == 0) {
        [self.classifyName shakeAlert];
        [self showText:@"分类名不能为空"];
        return;
    }
    if (actionType == 0) {
        NSDictionary *dic = @{@"p_account":self.model.account, @"classify_name":name};
        [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:@"MobilePort/index/AddArticleClassify.html" parameters:dic finish:^(BOOL seccess, NSString *error, id obj) {
            if (seccess) {
                NSNumber *number = obj[@"code"];
                if (number.intValue == 1) {
                    [self showText:obj[@"msg"]];
                } else {
                    [self showText:@"添加分类成功！"];
                    [self popViewController];
                }
            } else {
                [self showText:error];
            }
        }];
    } else {
        NSDictionary *dic = @{@"p_account":self.model.account,
                              @"a_classify_id":[NSString stringWithFormat:@"%lu",(unsigned long)self.acModel.aClassifyId],
                              @"a_classify_name":name};
        [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:@"MobilePort/index/ModifyArticleClassify.html" parameters:dic finish:^(BOOL seccess, NSString *error, id obj) {
            if (seccess) {
                NSNumber *number = obj[@"code"];
                if (number.intValue == 1) {
                    [self showText:obj[@"msg"]];
                } else {
                    [self showText:@"修改分类成功！"];
                    [self popViewController];
                }
            } else {
                [self showText:error];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
